package com.cskaoyan.service;
/*
 * @author 魏骏杰
 */

import com.alibaba.excel.EasyExcel;
import com.cskaoyan.bean.*;
import com.cskaoyan.bean.keyword.KeyWordBO;
import com.cskaoyan.bean.keyword.KeyWordVO;
import com.cskaoyan.bean.sysmanage.KeywordExcel;
import com.cskaoyan.bean.user.SearchHistory;
import com.cskaoyan.bean.user.SearchHistoryExample;
import com.cskaoyan.bean.wx_search.SearchIndexVo;
import com.cskaoyan.exception.MarketException;
import com.cskaoyan.listener.KeywordExcelListener;
import com.cskaoyan.mapper.KeyWordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.mapper.UserMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class KeyWordServiceImpl implements KeyWordService {

    @Autowired
    KeyWordMapper keyWordMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public List<KeyWordVO> getKeyWordList(KeyWordBO keyWordBO) {
        if (keyWordBO.getKeyword() == null && keyWordBO.getUrl() == null) {
            return keyWordMapper.findAllKeyWord();
        } else if (keyWordBO.getKeyword() == null) {
            keyWordBO.setKeyword("");
        } else if (keyWordBO.getUrl() == null) {
            keyWordBO.setUrl("");
        }
        List<KeyWordVO> keyWordVOS = keyWordMapper.findTargetByKeywordOrUrl(keyWordBO);
        return keyWordVOS;
    }

    @Override
    public KeyWordVO createNewKeyWord(KeyWordBO keyWordBO) {
        keyWordBO.setIsHotEscape(keyWordBO.getIsHot() ? 1 : 0);
        keyWordBO.setIsDefaultEscape(keyWordBO.getIsDefault() ? 1 : 0);
        keyWordMapper.insertKeyWord(keyWordBO);
        return keyWordMapper.findTargetByKeywordOrUrl(keyWordBO).get(0);
    }

    @Override
    public KeyWordVO updateNewKeyWord(KeyWordBO keyWordBO) {
        keyWordBO.setIsHotEscape(keyWordBO.getIsHot() ? 1 : 0);
        keyWordBO.setIsDefaultEscape(keyWordBO.getIsDefault() ? 1 : 0);
        keyWordMapper.updateKeyWord(keyWordBO);
        return keyWordMapper.findTargetByKeywordOrUrl(keyWordBO).get(0);
    }

    @Override
    public void deleteKeyWord(KeyWordBO keyWordBO) {
        keyWordMapper.deleteKeyWord(keyWordBO);
    }

    @Override
    public boolean hasExistence(KeyWordBO keyWordBO) {
        int code = keyWordMapper.selectByKeyWord(keyWordBO);
        return code == 1;
    }

    @SneakyThrows
    @Override
    public void updateKeywordByKeyword(Keyword keyword) {
        int affectedRows = keyWordMapper.updateKeyWordByKeyword(keyword);
        if(affectedRows != 1) {
            throw new MarketException("更新失败");
        }

    }

    @Override
    public SearchIndexVo selectSearchIndexVo(String username) {
        List<Keyword> keywordList = keyWordMapper.selectAllHotKeyword();
        Keyword defaultKeyword = keyWordMapper.selectKeywordById(2);

        ArrayList<SearchIndexVo.HistoryKeywordListDTO> historyKeywordListDTOS = new ArrayList<>();
        if(username != null) {
            UserExample example = new UserExample();
            UserExample.Criteria userCriteria = example.createCriteria();
            userCriteria.andUsernameEqualTo(username);
            List<User> userList = userMapper.selectByExample(example);
            if(userList != null && userList.size() >0) {
                SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
                SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
                searchHistoryExample.setDistinct(true);
                criteria.andUserIdEqualTo(userList.get(0).getId());
                searchHistoryExample.setOrderByClause("id desc");
                List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
                for (SearchHistory searchHistory : searchHistories) {
                    String keyword = searchHistory.getKeyword();
                    SearchIndexVo.HistoryKeywordListDTO historyKeywordListDTO = new SearchIndexVo.HistoryKeywordListDTO();
                    historyKeywordListDTO.setKeyword(keyword);
                    historyKeywordListDTOS.add(historyKeywordListDTO);
                }
            }

        }

        SearchIndexVo searchIndexVo = new SearchIndexVo();
        searchIndexVo.setDefaultKeyword(defaultKeyword);
        searchIndexVo.setHotKeywordList(keywordList);
        searchIndexVo.setHistoryKeywordList(historyKeywordListDTOS);

        return searchIndexVo;
    }

    @SneakyThrows
    @Override
    public void importKeywordData(MultipartFile file, KeyWordService keyWordService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, KeywordExcel.class,new KeywordExcelListener(keyWordService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new MarketException("文件读取失败");
        }
    }

    @Override
    public int selectKeyword(String keyword) {
        int num = keyWordMapper.selectKeyword(keyword);
        return num;
    }

    @Override
    public void addKeyword(Keyword keyWord) {
        keyWordMapper.insertKeyword(keyWord);
    }
}
